//
//  OpinionViewController.m
//  LiverDisease
//
//  Created by 朱亚杰 on 16/4/20.
//  Copyright © 2016年 dongshangtong. All rights reserved.
//

#import "OpinionViewController.h"

@interface OpinionViewController () <UITextViewDelegate>

@property (weak, nonatomic) UILabel *placeholderLabel;

@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation OpinionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"意见反馈";
    self.view.backgroundColor = [UIColor whiteColor];
    
    _textView.layer.borderWidth = 1;
    _textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _textView.layer.cornerRadius = 4;
    _textView.layer.masksToBounds = YES;
    UILabel *placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, kScreenWidth - 24 -10, 45)];
    placeholderLabel.font = SubTitleFont;
    placeholderLabel.numberOfLines = 0;
    placeholderLabel.textColor = LigthGray;
    placeholderLabel.font= [UIFont systemFontOfSize:16];
    NSString *str = @"请对我们提出您宝贵的建议，您的建议是我们前进的动力！";
    placeholderLabel.attributedText = [self getAttributedText:str];
    placeholderLabel = placeholderLabel;
    placeholderLabel.enabled = NO;//lable必须设置为不可用
    [_textView addSubview:placeholderLabel];
    _placeholderLabel = placeholderLabel;
//    _placeholderLabel.hidden = YES;
    
    _textView.delegate = self;
    _textView.tintColor = [UIColor lightGrayColor];
    [_textView becomeFirstResponder];
    
    
    //    4. 设置提交按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat buttonY = 300;
    button.frame = CGRectMake(SCREEN_WIDTH/2 -60, buttonY, 120, 44);
    button.titleLabel.font = BigTitleFont;
    [button setTitleColor:[UIColor colorWithRGBValue:0x0092F0] forState:UIControlStateNormal];
    button.layer.cornerRadius = 4;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1;

    button.layer.borderColor = [UIColor colorWithRGBValue:0x0092F0].CGColor;
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(submitOpinion) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
}

#pragma mark - textView 代理方法
-(void)textViewDidChange:(UITextView *)textView {
    
    if (textView.text.length == 0) {
        
        _placeholderLabel.hidden = NO;
    }else{
        _placeholderLabel.hidden = YES;
    }   
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
    
}

#pragma mark - 返回有属性的文字
- (NSAttributedString *)getAttributedText:(NSString *)text {

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 7;// 字体的行间距
    
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15.0],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (void)submitOpinion {
    
    if([[_textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        
        [self presentFailureTips:@"请填写您的建议"];
        return ;
    }
    
    [self presentLoadingTips:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self presentSuccessTips:@"提交建议成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissTips];
            [self.navigationController popViewControllerAnimated:YES];
        });
    });
}

@end
